<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/4
 * Time: 10:00
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Yuereport extends Base
{
    /**
     * 约稿举报类型列表
     * @return mixed|\think\response\Json
     */
    public function reportType(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('yue_report_type')
                ->where('yue_report_del', 1)
                ->count();// 获取总条数
            $lists = Db::name('yue_report_type')
                ->where('yue_report_del', 1)
                ->field('yue_report_type_id, yue_report_type_name, yue_report_type_time, yue_report_type_updatetime')
                ->page($Nowpage,$limits)
                ->order('yue_report_type_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('yuereport/reporttype');
    }
    
    /**
     * 添加约稿举报分类
     * @return mixed|\think\response\Json
     */
    public function typeAdd(){
        if(Request::instance()->isPost()){
            $yue_report_type_name = input('yue_report_type_name');
            $insert = [
                'yue_report_type_name' => $yue_report_type_name,
                'yue_report_type_time' => date('Y-m-d H:i:s'),
                'yue_report_type_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_yue_report_type')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('yuereport/typeadd');
    }
    
    /**
     * 编辑分类
     * @return mixed|\think\response\Json
     */
    public function typeEdit(){
        $yue_report_type_id = input('yue_report_type_id');
        if(Request::instance()->isPost()){
            $yue_report_type_name = input('yue_report_type_name');
            $update = [
                'yue_report_type_name' => $yue_report_type_name,
                'yue_report_type_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_yue_report_type')->where('yue_report_type_id', $yue_report_type_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $type = Db::table('up_yue_report_type')->field('yue_report_type_name')->where('yue_report_type_id', $yue_report_type_id)->find();
        $this->assign([
            'type' => $type,
            'yue_report_type_id' => $yue_report_type_id
        ]);
        return $this->fetch('yuereport/typeedit');
    }
    
    /**
     * 删除分类
     * @return \think\response\Json
     */
    public function typeDel(){
        $yue_report_type_id = input('id');
        $res = Db::table('up_yue_report_type')->where('yue_report_type_id', $yue_report_type_id)->setField('yue_report_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 举报列表
     * @return mixed|\think\response\Json
     */
    public function reportIndex(){
        if(request()->isAjax ()){
            $user_keyword = input('user_keyword'); // 举报人-手机号、ID、昵称
            $other_user_keyword = input('other_user_keyword'); // 被举报人-手机号、ID、昵称
            $report_type = input('report_type'); // 分类
            $report_start_time = input('report_start_time');
            $report_end_time = input('report_end_time');
            $report_status = input('report_status');
            $where = [];
            if(isset($user_keyword) && $user_keyword != ''){
                $where['uuu1.user_phone|uuu1.user_id|uuu1.user_username'] = ['like', '%' . $user_keyword . '%'];
            }
            if(isset($other_user_keyword) && $other_user_keyword != ''){
                $where['uuu2.user_phone|uuu2.user_id|uuu2.user_username'] = ['like', '%' . $other_user_keyword . '%'];
            }
            if(isset($report_type) && $report_type != ''){
                $where['yue_report_type_id'] = $report_type;
            }
            if(isset($report_status) && $report_status != ''){
                $where['report_status'] = $report_status;
            }
            if(isset($report_start_time) && $report_start_time != '' && isset($report_end_time) && $report_end_time != ''){
                $where['report_time'] = ['between', [$report_start_time, $report_end_time]];
            }
            if(isset($report_start_time) && $report_start_time != '' && isset($report_end_time) && $report_end_time == ''){
                $where['report_time'] = ['>', $report_start_time];
            }
            if(isset($report_start_time) && $report_start_time == '' && isset($report_end_time) && $report_end_time != ''){
                $where['report_time'] = ['<', $report_end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('yue_report')
                ->join('up_u_user uuu1', 'uuu1.user_id = report_user_id', 'LEFT')
                ->join('up_u_user uuu2', 'uuu2.user_id = report_other_user_id', 'LEFT')
                ->join('up_yue_report_type', 'yue_report_type_id = report_yue_type_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('yue_report')
                ->join('up_u_user uuu1', 'uuu1.user_id = report_user_id', 'LEFT')
                ->join('up_u_user uuu2', 'uuu2.user_id = report_other_user_id', 'LEFT')
                ->join('up_yue_report_type', 'yue_report_type_id = report_yue_type_id', 'LEFT')
                ->where($where)
                ->field('report_id, uuu1.user_username my_name, uuu1.user_id my_id, uuu1.user_phone my_phone, 
                yue_report_type_name, report_time, uuu2.user_username other_name, uuu2.user_id other_id, uuu2.user_phone other_phone, 
                report_yue_id, report_status')
                ->page($Nowpage,$limits)
                ->order('report_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $type = Db::table('up_yue_report_type')->where('yue_report_del', 1)->field('yue_report_type_id, yue_report_type_name')->select();
        $this->assign('type', $type);
        return $this->fetch('yuereport/reportindex');
    }
    
    /**
     * 举报详情
     * @return mixed
     */
    public function reportDetail(){
        $report_id = input('report_id');
        $detail = Db::table('up_yue_report')
            ->join('up_u_user uuu1', 'uuu1.user_id = report_user_id', 'LEFT')
            ->join('up_u_user uuu2', 'uuu2.user_id = report_other_user_id', 'LEFT')
            ->join('up_yue_report_type', 'yue_report_type_id = report_yue_type_id', 'LEFT')
            ->join('up_yue', 'yue_id = report_yue_id', 'LEFT')
            ->where('report_id', $report_id)
            ->field('report_id, uuu1.user_username my_name, uuu1.user_id my_id, uuu1.user_phone my_phone, 
                yue_report_type_name, report_time, uuu2.user_username other_name, uuu2.user_id other_id, uuu2.user_phone other_phone, 
                yue_id, report_status, yue_title, yue_day_num, yue_price, yue_explain, yue_images')
            ->find();
        if($detail['yue_images']){
            $images = trim($detail['yue_images'], ',');
            $detail['yue_images'] = explode(',', $images);
        }
        $this->assign('detail', $detail);
        return $this->fetch('yuereport/reportdetail');
    }
    
    /**
     * 处理
     * @return \think\response\Json
     */
    public function reportDone(){
        $report_id = input('id');
        $res = Db::table('up_yue_report')->where('report_id', $report_id)->setField('report_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '处理成功']);
        }else{
            return json(['code' => 100, 'msg' => '处理失败']);
        }
    }
    
    /**
     * 删除举报
     * @return \think\response\Json
     */
    public function reportDel(){
        $report_id = input('id');
        $res = Db::table('up_yue_report')->delete($report_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}